package com.training.java.dao;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DaoDemo {

	public static void main(String[] args)
	{
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "password");
			statement=connection.createStatement();
			resultSet= statement.executeQuery("select * from countries");
			
			while(resultSet.next())
			{
				String country_id= resultSet.getString("country_id");
				String country_name=resultSet.getString("country_name");
				int region_id= resultSet.getInt("region_id");
				
				System.out.println("country_id: "+ country_id);
				System.out.println("country_name: "+ country_name);
				System.out.println("region_id: "+ region_id);
				System.out.println("***************************");
								
			}
			
					
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			System.out.println("Could not connect to DB"); 
		}
		finally
		{
			if (resultSet!=null)
			{
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(statement!=null)
			{
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection!=null)
			{
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		

	}

}
